# -*- coding:utf-8 -*-
from paddlenlp import Taskflow
import json
import csv


def ner():
    ner = Taskflow('ner')
    file_name = './data/chapter10.txt'
    fo = open(file_name, 'r', encoding='utf-8')
    str1_count = 0
    entity_set = ['人物类_概念', '人物类_实体', '术语类_生物体' '物体类', '术语类', '组织机构类', '作品类_概念', '作品类_实体']
    for str1 in fo.readlines():
        str1_count += 1
        # print('str是：', str1)
        str1_ner = ner(str1)
        # print(str1_ner)
        i=0
        len1 = len(str1_ner)
        # print(len1)
        while(i<len1):
            # print(str1_ner[i][1])
            if str1_ner[i][1] in entity_set:
                dict[str1_ner[i][0]] = str1_ner[i][1]  # 添加到字典
            i += 1
    print('共有%d行文本' % str1_count)
    print(dict)


def ner_csv():
    ner = Taskflow('ner')
    file_name = './data/appendix1.txt'
    fo = open(file_name, 'r', encoding='utf-8')
    str1_count = 0
    entity_set = ['人物类_概念', '人物类_实体', '术语类_生物体', '物体类', '术语类', '组织机构类', '作品类_概念', '作品类_实体', '场景事件','时间类','场所类','世界地区类']
    with open('appendix1.csv', 'a+', encoding='utf-8') as f:
        header_column = ['内容', '类别']
        writer = csv.writer(f, delimiter=',')
        writer.writerow(header_column)  # 写入表头
        for str1 in fo.readlines():
            str1_count += 1
            # print('str是：', str1)
            str1_ner = ner(str1)
            # print(str1_ner)
            i = 0
            len1 = len(str1_ner)
            # print(len1)
            while i < len1:
                # print(str1_ner[i][1])
                if str1_ner[i][1] in entity_set:
                    print(str1_ner[i])
                    writer.writerow(str1_ner[i])
                i += 1
    print('共有%d行文本' % str1_count)
    f.close()
    fo.close()


def sentence_and_ner():
    ner = Taskflow('ner')
    # print(ner('するとあなたにもっとマッチした'))
    file_name = './data/chapter10.txt'
    fo = open(file_name, 'r', encoding='utf-8')
    str1_count = 0
    for str1 in fo.readlines():
        str1_count += 1
        # print('str是：', str1)
        str1_ner = ner(str1)
        # print('实体识别后是：', str1_ner)
        json_ner = json.dumps(str1_ner, indent=4, ensure_ascii=False)
        with open('data/sentence_and_ner.json', 'a+', encoding='utf-8') as json_file:
            json_file.write(str1)
            json_file.write(json_ner + '\n')
    print('共有%d行文本' % str1_count)
def kb_txt_to_csv():
    fo = open('data/人工智能简史知识图谱.txt', 'r', encoding='utf-8')
    str1_count = 0
    with open('ai_brief_kb.csv', 'a+', encoding='utf-8') as f:
        header_column = ['实体', '属性', '值']
        writer = csv.writer(f, delimiter=',')
        writer.writerow(header_column)  # 写入表头
        for str_row in fo.readlines():
            for str_column in str_row:
                if str
    print('共有%d行文本' % str1_count)
    f.close()
    fo.close()
# print(c)
# json_ner = json.dumps(c, indent=4, ensure_ascii=False)
# with open('./data/sentence_and_ner.json', 'a+', encoding='utf-8') as json_file:
#     json_file.write(json_ner)

# print(json_ner)
# ner()
# ner_csv()
# qa = Taskflow("question_answering")
# qa("图灵是谁")